package com.yundingai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yundingai.domain.entity.UserFriend;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author liyuxiang
* @description 针对表【user_friend(好友关系表：记录用户的好友关系；互为好友查询：user_id与friend_id均符合的条目)】的数据库操作Mapper
* @createDate 2025-07-16 12:50:48
* @Entity com.yundingai.domain.entity.UserFriend
*/
@Mapper
public interface UserFriendMapper extends BaseMapper<UserFriend> {

  /**
   * 查询用户的好友ID列表（双向关系，只返回已通过的好友）
   */
  @Select("SELECT CASE WHEN user_id = #{userId} THEN friend_id ELSE user_id END AS friend_id " +
          "FROM user_friend " +
          "WHERE (user_id = #{userId} OR friend_id = #{userId}) and status = 1")
  List<Integer> findFriendIdsByUserId(@Param("userId") Integer userId);

  // 检查两个用户之间是否存在好友关系(已通过)
  @Select("SELECT COUNT(*) > 0 FROM user_friend WHERE " +
          "((user_id = #{userId1} AND friend_id = #{userId2}) OR " +
          "(user_id = #{userId2} AND friend_id = #{userId1})) and status = 1")
  boolean existsConnection(@Param("userId1") Integer userId1, @Param("userId2") Integer userId2);

  // 查询用户收到的好友请求(待审核)
  @Select("SELECT * FROM user_friend WHERE user_id = #{userId} AND status = 0")
  List<UserFriend> findPendingRequests(@Param("userId") Integer userId);

  // 查询两个用户之间的好友关系
  @Select("SELECT * FROM user_friend WHERE " +
          "(user_id = #{userId1} AND friend_id = #{userId2}) OR " +
          "(user_id = #{userId2} AND friend_id = #{userId1})")
  List<UserFriend> findRelationBetweenUsers(@Param("userId1") Integer userId1, @Param("userId2") Integer userId2);
}




